package org.xbill.DNS;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

public final class Address
{
  public static final int IPv4 = 1;
  public static final int IPv6 = 2;

  private static InetAddress addrFromRecord(String paramString, Record paramRecord)
    throws UnknownHostException
  {
    return InetAddress.getByAddress(paramString, ((ARecord)paramRecord).getAddress().getAddress());
  }

  public static int addressLength(int paramInt)
  {
    int i;
    if (paramInt == 1)
      i = 4;
    while (true)
    {
      return i;
      if (paramInt != 2)
        break;
      i = 16;
    }
    throw new IllegalArgumentException("unknown address family");
  }

  public static int familyOf(InetAddress paramInetAddress)
  {
    int i;
    if (paramInetAddress instanceof Inet4Address)
      i = 1;
    while (true)
    {
      return i;
      if (!(paramInetAddress instanceof Inet6Address))
        break;
      i = 2;
    }
    throw new IllegalArgumentException("unknown address family");
  }

  public static InetAddress[] getAllByName(String paramString)
    throws UnknownHostException
  {
    Object localObject;
    InetAddress localInetAddress;
    try
    {
      localInetAddress = getByAddress(paramString);
      localObject = new InetAddress[1];
      localObject[0] = localInetAddress;
      return localObject;
    }
    catch (UnknownHostException localUnknownHostException)
    {
      Record[] arrayOfRecord;
      InetAddress[] arrayOfInetAddress;
      int i;
      while (true)
      {
        arrayOfRecord = lookupHostName(paramString);
        arrayOfInetAddress = new InetAddress[arrayOfRecord.length];
        i = 0;
        if (i < arrayOfRecord.length)
          break;
        localObject = arrayOfInetAddress;
      }
      arrayOfInetAddress[i] = addrFromRecord(paramString, arrayOfRecord[i]);
      ++i;
    }
  }

  public static InetAddress getByAddress(String paramString)
    throws UnknownHostException
  {
    InetAddress localInetAddress;
    byte[] arrayOfByte1 = toByteArray(paramString, 1);
    if (arrayOfByte1 != null)
      localInetAddress = InetAddress.getByAddress(arrayOfByte1);
    while (true)
    {
      return localInetAddress;
      byte[] arrayOfByte2 = toByteArray(paramString, 2);
      if (arrayOfByte2 == null)
        break;
      localInetAddress = InetAddress.getByAddress(arrayOfByte2);
    }
    throw new UnknownHostException("Invalid address: " + paramString);
  }

  public static InetAddress getByAddress(String paramString, int paramInt)
    throws UnknownHostException
  {
    if ((paramInt != 1) && (paramInt != 2))
      throw new IllegalArgumentException("unknown address family");
    byte[] arrayOfByte = toByteArray(paramString, paramInt);
    if (arrayOfByte != null)
      return InetAddress.getByAddress(arrayOfByte);
    throw new UnknownHostException("Invalid address: " + paramString);
  }

  public static InetAddress getByName(String paramString)
    throws UnknownHostException
  {
    InetAddress localInetAddress1;
    InetAddress localInetAddress2;
    try
    {
      localInetAddress2 = getByAddress(paramString);
      localInetAddress1 = localInetAddress2;
      return localInetAddress1;
    }
    catch (UnknownHostException localUnknownHostException)
    {
      localInetAddress1 = addrFromRecord(paramString, lookupHostName(paramString)[0]);
    }
  }

  public static String getHostName(InetAddress paramInetAddress)
    throws UnknownHostException
  {
    Record[] arrayOfRecord = new Lookup(ReverseMap.fromAddress(paramInetAddress), 12).run();
    if (arrayOfRecord == null)
      throw new UnknownHostException("unknown address");
    return ((PTRRecord)arrayOfRecord[0]).getTarget().toString();
  }

  public static boolean isDottedQuad(String paramString)
  {
    int i = 1;
    if (toByteArray(paramString, i) != null);
    while (true)
    {
      return i;
      i = 0;
    }
  }

  private static Record[] lookupHostName(String paramString)
    throws UnknownHostException
  {
    Record[] arrayOfRecord;
    try
    {
      arrayOfRecord = new Lookup(paramString).run();
      if (arrayOfRecord == null)
        throw new UnknownHostException("unknown host");
    }
    catch (TextParseException localTextParseException)
    {
      throw new UnknownHostException("invalid name");
    }
    return arrayOfRecord;
  }

  private static byte[] parseV4(String paramString)
  {
    byte[] arrayOfByte2;
    byte[] arrayOfByte1 = new byte[4];
    int i = paramString.length();
    int j = 0;
    int k = 0;
    int l = 0;
    int i1 = 0;
    if (l >= i)
    {
      if (i1 == 3)
        break label177;
      arrayOfByte2 = null;
    }
    while (true)
    {
      while (true)
      {
        int i2;
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return arrayOfByte2;
              i2 = paramString.charAt(l);
              if ((i2 < 48) || (i2 > 57))
                break label114;
              if (k != 3)
                break;
              arrayOfByte2 = null;
            }
            if ((k <= 0) || (j != 0))
              break;
            arrayOfByte2 = null;
          }
          ++k;
          j = j * 10 + i2 - 48;
          if (j <= 255)
            break label200;
          arrayOfByte2 = null;
        }
        label114: if (i2 != 46)
          break label171;
        if (i1 != 3)
          break;
        arrayOfByte2 = null;
      }
      if (k != 0)
        break;
      arrayOfByte2 = null;
    }
    int i3 = i1 + 1;
    arrayOfByte1[i1] = (byte)j;
    j = 0;
    k = 0;
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              ++l;
              i1 = i3;
            }
            label171: arrayOfByte2 = null;
          }
          label177: if (k != 0)
            break;
          arrayOfByte2 = null;
        }
        arrayOfByte1[i1] = (byte)j;
        arrayOfByte2 = arrayOfByte1;
      }
      label200: i3 = i1;
    }
  }

  private static byte[] parseV6(String paramString)
  {
    int j;
    int k;
    int i1;
    int i2;
    int i3;
    int i4;
    byte[] arrayOfByte2;
    label110: label119: int i6;
    int i7;
    int i8;
    int i10;
    byte[] arrayOfByte1 = new byte[16];
    String[] arrayOfString = paramString.split(":", -1);
    int i = arrayOfString.length - 1;
    if (arrayOfString[0].length() == 0)
    {
      label212: byte[] arrayOfByte3;
      if ((i - 0 > 0) && (arrayOfString[1].length() == 0))
      {
        j = 0 + 1;
        if (arrayOfString[i].length() != 0)
          break label560;
        if ((i - j <= 0) || (arrayOfString[(i - 1)].length() != 0))
          break label110;
        k = i - 1;
        if (1 + k - j <= 8)
          break label119;
        arrayOfByte2 = null;
      }
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                return arrayOfByte2;
                arrayOfByte2 = null;
              }
              arrayOfByte2 = null;
            }
            int l = j;
            i1 = -1;
            i2 = 0;
            i3 = l;
            while (true)
            {
              while (true)
              {
                while (i3 > k)
                {
                  if ((i2 >= 16) || (i1 >= 0))
                    break label483;
                  arrayOfByte2 = null;
                }
                if (arrayOfString[i3].length() != 0)
                  break label212;
                if (i1 < 0)
                  break;
                arrayOfByte2 = null;
              }
              i10 = i2;
              ++i3;
              i1 = i10;
            }
            if (arrayOfString[i3].indexOf(46) < 0)
              break label332;
            if (i3 >= k)
              break;
            arrayOfByte2 = null;
          }
          if (i3 <= 6)
            break;
          arrayOfByte2 = null;
        }
        arrayOfByte3 = toByteArray(arrayOfString[i3], 1);
        if (arrayOfByte3 != null)
          break;
        arrayOfByte2 = null;
      }
      int i11 = 0;
      while (true)
      {
        if (i11 >= 4);
        int i12 = i2 + 1;
        arrayOfByte1[i2] = arrayOfByte3[i11];
        ++i11;
        i2 = i12;
      }
      label332: i4 = 0;
    }
    try
    {
      while (true)
      {
        while (true)
        {
          if (i4 >= arrayOfString[i3].length())
          {
            i6 = Integer.parseInt(arrayOfString[i3], 16);
            if (i6 > 65535)
              break label572;
            if (i6 >= 0)
              break label415;
            break label572:
          }
          int i5 = Character.digit(arrayOfString[i3].charAt(i4), 16);
          if (i5 >= 0)
            break;
          arrayOfByte2 = null;
        }
        ++i4;
      }
      label415: i7 = i2 + 1;
      label560: label572: label468: label483: i8 = (byte)(i6 >>> 8);
    }
    catch (NumberFormatException localNumberFormatException1)
    {
      try
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              arrayOfByte1[i2] = i8;
              i2 = i7 + 1;
              int i9 = (byte)(i6 & 0xFF);
              arrayOfByte1[i7] = i9;
              i10 = i1;
            }
            localNumberFormatException1 = localNumberFormatException1;
            arrayOfByte2 = null;
          }
          if (i1 >= 0)
          {
            int i13 = 16 - i2;
            System.arraycopy(arrayOfByte1, i1, arrayOfByte1, i1 + i13, i2 - i1);
            i3 = i1;
            if (i3 < i1 + i13)
              break;
          }
          arrayOfByte2 = arrayOfByte1;
        }
        arrayOfByte1[i3] = 0;
        ++i3;
      }
      catch (NumberFormatException localNumberFormatException2)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              break label468:
              k = i;
            }
            j = 0;
          }
          arrayOfByte2 = null;
        }
      }
    }
  }

  public static int[] toArray(String paramString)
  {
    return toArray(paramString, 1);
  }

  public static int[] toArray(String paramString, int paramInt)
  {
    Object localObject;
    byte[] arrayOfByte = toByteArray(paramString, paramInt);
    if (arrayOfByte == null)
    {
      localObject = null;
      return localObject;
    }
    int[] arrayOfInt = new int[arrayOfByte.length];
    int i = 0;
    while (true)
    {
      while (i >= arrayOfByte.length)
        localObject = arrayOfInt;
      arrayOfInt[i] = (0xFF & arrayOfByte[i]);
      ++i;
    }
  }

  public static byte[] toByteArray(String paramString, int paramInt)
  {
    byte[] arrayOfByte;
    if (paramInt == 1)
      arrayOfByte = parseV4(paramString);
    while (true)
    {
      return arrayOfByte;
      if (paramInt != 2)
        break;
      arrayOfByte = parseV6(paramString);
    }
    throw new IllegalArgumentException("unknown address family");
  }

  public static String toDottedQuad(byte[] paramArrayOfByte)
  {
    return (0xFF & paramArrayOfByte[0]) + "." + (0xFF & paramArrayOfByte[1]) + "." + (0xFF & paramArrayOfByte[2]) + "." + (0xFF & paramArrayOfByte[3]);
  }

  public static String toDottedQuad(int[] paramArrayOfInt)
  {
    return paramArrayOfInt[0] + "." + paramArrayOfInt[1] + "." + paramArrayOfInt[2] + "." + paramArrayOfInt[3];
  }
}